package com.pine.sunshineelder.checkin.web.handler;


import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pine.sunshineelder.checkin.ao.elder.ElderAddAo;
import com.pine.sunshineelder.checkin.ao.elder.ElderModAo;
import com.pine.sunshineelder.checkin.ao.elder.ElderModPasswordAo;
import com.pine.sunshineelder.checkin.service.ElderService;
import com.pine.sunshineelder.checkin.vo.elder.ElderRegisterVo;
import com.pine.sunshineelder.checkin.vo.elder.ElderRelationVo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dto.JSONResponse;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderEntity;


@RestController
@RequestMapping("/api")
public class ElderHandler implements StringHelper{

	@Autowired
	ElderService elderService;

	@PostMapping("/elder")
	public JSONResponse registerUser(@Valid ElderAddAo ao) throws Exception {
		ElderEntity ue = elderService.addElder(ao);
		return JSONResponse.ok(ElderRegisterVo.of(ue));
	}

	@DeleteMapping("/elder/{id}")
	public JSONResponse deleteUser( @PathVariable String id) throws Exception {
		elderService.deleteElder(id);
		return JSONResponse.ok();
	}

	@PutMapping("/elder/{id}")
	public JSONResponse modifyUser(@PathVariable String id,@Valid ElderModAo ao) throws Exception {
		elderService.modifyElder(id, ao);
		return JSONResponse.ok();
	}
	@PutMapping("/elder/mop/{id}")
	public JSONResponse modifyUser(@PathVariable String id, ElderModPasswordAo ao) throws Exception {
		elderService.modifyPassword(id, ao);
	
		return JSONResponse.ok();
	}
	@GetMapping("/elder/{id}")
    public JSONResponse listOne(@PathVariable String id)throws Exception{
    	ElderEntity queryById = elderService.queryById(id);
		return JSONResponse.ok(ElderRegisterVo.of(queryById));
    	
    }
	@GetMapping("/elre/{id}")
    public JSONResponse listRelative(@PathVariable String id)throws Exception{
    	ElderEntity queryById = elderService.queryById(id);
		return JSONResponse.ok(ElderRelationVo.of(queryById));
    	
    }
	@GetMapping("/elder")
	public JSONResponse listElder(String key, String pageNo, String pageSize) throws Exception {
		Integer pageNoUse = parseInt(pageNo, 1);
		Integer pageSizeUse = parseInt(pageSize,10);
		QueryResult<ElderEntity> result = elderService.queryByKey(key, pageNoUse, pageSizeUse);
		return JSONResponse.ok(result.convert(ElderRegisterVo::of));
	}

}
